<?php include('Header.php');
include('loadvalues.php');
        if ($_SESSION['logueado'] == 1)
        {
                //select para ver la cantidad de acciones del usuario.
                $user = $_SESSION["user"];
     $select = mysql_query("SELECT UserAssets.IDCompany, UserAssets.Name, UserAssets.Quantity, UserAssets.Fecha, UserAssets.Hora, UserAssets.Precio_Compra, CompanyAssets.Value FROM UserAssets INNER JOIN
                                                          CompanyAssets ON UserAssets.IDCompany = CompanyAssets.IDCompany WHERE UserAssets.Name = '$user' AND CompanyAssets.IDCompany = '{$_GET['c']}'");
                $row = @mysql_fetch_array($select);
                echo "<div id='contenido_central'>";
                        echo "<div id='header_logos'>";
                                echo"<h4> Assets:  {$row['Quantity']}<br> 
										  Value: {$row['Value']} <br> 
										  Purchase price: {$row['Precio_Compra']} </br>
										  Date and time of purchase: {$row['Fecha']} || {$row['Hora']}<br>
										  </h4>";
								 
                                echo "<img src='./images/Companies/{$_GET['c']}.png'></img>";
                        echo "</div>";
                echo "<form action='' method='POST'>";
                        echo "<input id='text_QAssets' type='text' name='cantidad' value='0'>"; //Cantidad de acciones que se quieren vender
                        echo "<input class='botonventa' style='position:relative; left:43%; margin-top:3%;' type='submit' name='Sale' value='Sale'><br /><br />";
                echo "</form>";
                //Vendemos!
                if (isset($_POST['cantidad'])) {
                        $cantidad = $_POST['cantidad'];
                        if ($cantidad > $row['Quantity'])
                        {
                                echo "<p class='trans_error'>You can't sell more assets than you have</p>";
                        } else {
                                //El primer update nos actualiza las acciones y el segundo el dinero.
                                if ($cantidad > 0){
                                
                                $diferencia = ($row['Quantity'] - $cantidad);
                                $update = mysql_query("UPDATE UserAssets SET Quantity = $diferencia WHERE Name = '$user' AND IDCompany = '{$_GET['c']}'");
                                
                                if ($diferencia == 0 ){
                                
                                $delete = mysql_query(" delete from UserAssets WHERE Name = '$user' AND IDCompany = '{$_GET['c']}'");
                                
                                }
                                
                                $updateMoney = mysql_query("UPDATE USERS SET Money = Money + ({$row['Value']} * '$cantidad') WHERE Name = '$user'");
                                echo "<p class='trans_ok'>transaction ok, you sold {$cantidad} assets</p>";
                echo "<script language='JavaScript'>";
                        echo"setTimeout('document.location.href =\'./portfolio.php\'',1000)"; 
                echo "</script>";       
                                
                                }else{
                                
                                echo "<p class='trans_error'> Wrong quantity ! </p>";
                                
                                }
                        }
                }
                echo "</div>";
        } else {
                $_SESSION['logueado'] = 0;
                echo "<script language='JavaScript'>";
                        echo"document.location.href = './index.php'"; 
                echo "</script>";       
        }
include('footer.php');
?>